<template>
    <el-row class="login-container">
        <el-col :lg="16" :md="12" class="left">
            <div>
                <div>欢迎光临</div>
                <div>xxxxxxxxxxxxxxxxxxxxxxxxxx</div>
            </div>
        </el-col>
        <el-col :lg="8" :md="12" class="right">
            <h2>欢迎回来</h2>
            <div>
                <span></span>
                <span>账号密码登录</span>
                <span></span>
            </div>
            <el-form ref="ruleFormRef" :rules="rules" :model="form" class="w-[250px]">
                <el-form-item prop="username">
                    <el-input v-model="form.username" placeholder="请输入用户名" :prefix-icon="User">
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" show-password v-model="form.password" placeholder="请输入密码"
                        :prefix-icon="Lock" />
                </el-form-item>
                <el-form-item>
                    <el-button round class="w-[250px]" color="#626aef" type="primary" @click="onSubmit"
                        :loading="loading">登 录</el-button>
                </el-form-item>
            </el-form>

        </el-col>
    </el-row>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { totast } from '@/composables/until.js'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store'

const userStore = useUserStore()

const router = useRouter()
const form = ref({
    username: '',
    password: '',

})
const rules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
    ],
}

const ruleFormRef = ref(null)
const loading = ref(false)
const onSubmit = () => {
    ruleFormRef.value.validate((valid) => {
        if (!valid) {
            return false
        }
        console.log(form.value);
        loading.value = true
        userStore.login(form.value).then(res => {
            totast('登录成功')
            // 跳转至首页
            router.push('/')

        }).finally(() => {
            loading.value = false
        })
    })

}
// 监听回车事件
function onKeyUp(e) {
    console.log(e.key);
    if (e.key == 'Enter') onSubmit()
}
// 页面初始
onMounted(() => {
    document.addEventListener('keyup', onKeyUp)
})
onBeforeUnmount(() => {
    document.removeEventListener('keyup', onKeyUp)
})
</script>

<style scoped>
.login-container {
    @apply bg-blue-600 min-h-screen;
}

.login-container .left,
.login-container .right {
    @apply flex items-center justify-center;
}

.login-container .right {
    @apply bg-light-400 flex-col;
}

.login-container .left>div>div:first-child {
    @apply text-light-100 text-3xl mb-3;
}

.login-container .left>div>div:last-child {
    @apply text-gray-100;
}

.login-container .right>h2 {
    @apply text-4xl font-bold;
}

.login-container .right>div {
    @apply flex items-center justify-center my-4 text-gray-200 space-x-2;
}

.login-container .right>div>span:first-child,
.login-container .right>div>span:last-child {
    @apply bg-gray-200 w-16 h-[1px];
}
</style>